package com.hfuu.service.impl;

import com.hfuu.dao.read.custom.TbCheckDataReadDao;
import com.hfuu.dao.write.custom.TbCheckDataWriteDao;
import com.hfuu.enums.system.ExceptionEnum;
import com.hfuu.exceptions.ServiceExceptionSpec;
import com.hfuu.model.po.TbCheckData;
import com.hfuu.model.po.TbCheckDataExample;
import com.hfuu.service.check.FileDataService;
import com.hfuu.service.check.request.FileDataRequest;
import com.hfuu.service.check.response.FileDataResponse;
import com.hfuu.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件管理类
 */
@Service
public class FileDataServiceImpl implements FileDataService {

    @Value("${filePath}")
    private String filePath;

    @Value("${servicePath}")
    private String servicePath;

    @Autowired
    private TbCheckDataReadDao tbCheckDataReadDao;

    @Autowired
    private TbCheckDataWriteDao tbCheckDataWriteDao;


    @Override
    public String registerOrginzerCheck(FileDataRequest request)
            throws ServiceExceptionSpec, IOException {

        if (request.getFile() == null ||
                request.getFile().isEmpty()
                || request.getUserId() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        //上传并获得文件名
        String fileName = FileUtils.upload(request.getFile(), filePath);

        //文件路径保存到数据库
        TbCheckData tbCheckData = new TbCheckData();
        tbCheckData.setUserId(request.getUserId());
        tbCheckData.setFilePath(filePath + fileName);
        tbCheckData.setType(request.getType());
        tbCheckData.setFileName(fileName);
        tbCheckDataWriteDao.insertSelective(tbCheckData);

        //文件下载回显
        return fileName;
    }

    @Override
    public Integer deleteOrginzeData(String fileName) throws ServiceExceptionSpec {

        if (StringUtils.isEmpty(fileName)) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        TbCheckDataExample example = new TbCheckDataExample();
        TbCheckDataExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andFilePathLike("%" + fileName + "%");

        TbCheckData tbCheckData = new TbCheckData();
        tbCheckData.setDisabled(1);
        return tbCheckDataWriteDao.updateByExampleSelective(tbCheckData, example);
    }

    @Override
    public List<FileDataResponse> getAllOrginzeData(FileDataRequest request) throws ServiceExceptionSpec {

        if (request.getType() == null || request.getUserId() == null
                || request.getFileType() == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        TbCheckDataExample example = new TbCheckDataExample();
        TbCheckDataExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0).andUserIdEqualTo(request.getUserId())
                .andTypeEqualTo(request.getType());
        List<TbCheckData> rList = tbCheckDataReadDao.selectByExample(example);

        if (CollectionUtils.isEmpty(rList)) {
            return new ArrayList<>();
        }
        if (request.getFileType().equals(1)) {
            rList = rList.stream().filter(elem -> elem.getFilePath().contains(".png")
                    || elem.getFilePath().contains(".jpg")).collect(Collectors.toList());
        } else {
            rList = rList.stream().filter(elem -> !elem.getFilePath().contains(".png")
                    && !elem.getFilePath().contains(".jpg")).collect(Collectors.toList());
        }
        return rList.stream().map(elem -> new FileDataResponse(elem.getFileName(),
                servicePath + "?filePath="
                        + elem.getFilePath().substring(0, elem.getFilePath().lastIndexOf("/") + 1)
                        + "&fileName=" + elem.getFileName(), elem.getId()))
                .collect(Collectors.toList());
    }

    @Override
    public org.springframework.http.ResponseEntity<byte[]>  getDataSource(Integer id) throws ServiceExceptionSpec {

        if (id == null) {
            throw new ServiceExceptionSpec(ExceptionEnum.USER_BASE_PARAMS_ILLEGAL);
        }
        TbCheckData tbCheckData = tbCheckDataReadDao.selectByPrimaryKey(id);
        return FileUtils.downLoad(tbCheckData.getFilePath(), tbCheckData.getFileName());
    }

    @Override
    public ResponseEntity<byte[]> getPicFile(String filePath, String fileName) {
        return FileUtils.downLoad1(filePath, fileName);
    }
}
